package kr.ragne.framework;

import java.util.ArrayList;

import kr.ragne.letsparis.GameState;
import kr.ragne.letsparis.Player;
import kr.ragne.letsparissql.DBHelper;
import kr.ragne.letsparitime.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
	private GameViewThread mThread;
	private GameState mGameState;
	private Player player;
	private IState mState;
	private boolean isBtnDown;
	private boolean isBtnUP;
	private int mX;
	private int mY;
	private float fY = 5;
	public ArrayList<String> mSaveList; // 게임 저장
	private DBHelper mHelper;
	private Activity mContext;
	MediaPlayer mSoundBackGround;

	private SurfaceHolder mHolder = null;

	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);

		// 키 입력 처리를 받기
		setFocusable(true);
		mHolder = getHolder();
		mHolder.addCallback(this);
		mThread = new GameViewThread(getHolder(), this);
		mHelper = new DBHelper(context);
		mContext = (Activity) context;
		AppManager.getInstance().setGameView(this);
		AppManager.getInstance().setResources(getResources());
		mSoundBackGround = MediaPlayer.create(mContext, R.raw.bumblebee);
		mGameState = new GameState(mContext, mHelper, mSoundBackGround);
		ChangeGameState(mGameState);

	}

	@Override
	public void draw(Canvas canvas) {
		if (canvas != null) {
			canvas.drawColor(Color.BLACK);
			mState.Render(canvas);
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
//		DisplayMetrics dm = new DisplayMetrics();
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// 스레드를 실행 상태로 만듬
		mThread.setRunning(true);
		// 스레드 실행
		mThread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		mThread.setRunning(false);
		while (retry) {
			try {
				mThread.join();
				retry = false;
			} catch (InterruptedException e) {
				// 스레드가 종료되도록 계속 시도
			}
		}
	}

	public void Update() {
		mState.Update();
	}

	private class TouchThread extends Thread {

		@Override
		public void run() {
			mX = player.getX();
			mY = player.getY();
			// fY = fY + gravity;
			if (isBtnDown) {
				while (isBtnDown) {
					try {
						if (player.getY() < 0) {
							player.SetPosition(mX, mY);
							player.boundBox.set(player.getX(), player.getY(),
									player.getX() + 62, player.getY() + 62);
						} else {
							player.SetPosition(mX, mY -= fY);
							player.boundBox.set(player.getX(), player.getY(),
									player.getX() + 62, player.getY() + 62);
						}
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

			if (isBtnUP) {
				while (isBtnUP) {
					try {
						if (player.getY() + 60 > 320) {
							player.SetPosition(mX, mY);
							player.boundBox.set(player.getX(), player.getY(),
									player.getX() + 62, player.getY() + 62);
						} else {
							player.SetPosition(mX, mY += fY);
							player.boundBox.set(player.getX(), player.getY(),
									player.getX() + 62, player.getY() + 62);
						}
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		TouchThread thread = new TouchThread();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			AppManager.start = true;
			if (AppManager.start) {
				if (AppManager.isFlag) {
					Log.d("TouchEvent", "Touch_down");
					isBtnDown = true;
					isBtnUP = false;
					mSoundBackGround.start();
					thread.start();
					break;
				} else{
					AppManager.getInstance().getActivity(mContext);
					AppManager.start = false;
				}
					
			}

		case MotionEvent.ACTION_UP:
			isBtnDown = false;
			isBtnUP = true;
			Log.d("TouchEvent", "Touch_up" + isBtnDown);
			break;
		}
		return true;
	}

	public void ChangeGameState(IState state) {
		if (mState != null)
			mState.Destroy();
		state.Init();
		mState = state;
		player = mGameState.getPlayer();
		mX = player.getX();
		mY = player.getY();
	}

}
